package com.synergy.stockflow.warehouse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.synergy.stockflow.warehouse.entity.InventoryCount;
import com.synergy.stockflow.warehouse.entity.InventorySnapshot;
import com.synergy.stockflow.warehouse.service.InventoryCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/inventory")
public class InventoryCountController {
    @Autowired
    private InventoryCountService inventoryCountService;
    //查询数据库
    @GetMapping("/trees")
    private Map<String, Object> TreesAll() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<InventoryCount> inventoryCounts = inventoryCountService.list();
            result.put("success", true);
            result.put("data", inventoryCounts);
            result.put("message", "查询成功");
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "查询失败:" + a.getMessage());
        }
        return result;
    }

    @GetMapping
    public Map<String, Object> sag(
            @RequestParam(name = "current", defaultValue = "1") int current,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "countId", required = false) String countId,
            @RequestParam(name = "countName", required = false) String countName
    ) {
        Map<String, Object> result = new HashMap<>();
        try {
            QueryWrapper<InventoryCount> queryWrapper = new QueryWrapper<>();
            if (countId != null && countId.isEmpty()) {
                queryWrapper.lambda().like(InventoryCount::getWarehouseId, countId);
            }
            if (countName != null && countName.isEmpty()) {
                queryWrapper.lambda().like(InventoryCount::getCountName, countName);
            }
            Page<InventoryCount> page = new Page<>(current, size);
            result.put("success", true);
            result.put("total", page.getPages());
            result.put("page", page.getPages());
            result.put("date", page.getRecords());
        } catch (Exception a) {
            result.put("success", false);
            result.put("massage", "分页查询失败:" + a.getMessage());
        }
        return result;
    }

    //新增
    @PostMapping("/insect")
    public Map<String, Object> insect(@RequestBody InventoryCount inventoryCount) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date now = new Date();
            inventoryCount.setCreateTime(now);
            inventoryCount.setUpdateTime(now);

            boolean success = inventoryCountService.save(inventoryCount);
            result.put("success", success);
            result.put("message", success ? "新增成功" : "新增失败");
            result.put("countId",inventoryCount.getCountId());
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增异常" + e.getMessage());
        }
        return result;
    }

    //修改
    @PutMapping("/update")
    private Map<String, Object> update(@RequestBody InventoryCount inventoryCount) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (inventoryCount.getCountId() == null || inventoryCount.getCountId().isEmpty()) {
                result.put("success", false);
                result.put("massage", "盘点Id不能为空");
            }
            inventoryCount.setUpdateTime(new Date());
            boolean success = inventoryCountService.updateById(inventoryCount);
            result.put("success", success);
            result.put("message", success ? "修改成功" : "修改失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "修改异常" + e.getMessage());
        }
        return result;
    }

    //删除
    @DeleteMapping("/{id}")
    private Map<String, Object> detele(@PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = inventoryCountService.removeById(id);
            result.put("success", success);
            result.put("message", success ? "删除成功" : "删除失败");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除异常" + e.getMessage());
        }
        return result;
    }
}
